﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Function
{
    public static class ExtensionMethod
    {
        /// <summary>
        /// 编码
        /// </summary>
        public static Encoding encode = getEncode();
        /// <summary>
        /// 密钥
        /// </summary>
        public const string Aeskey = Method.Aeskey;
        /// <summary>
        /// 向量
        /// </summary>
        public const string AesVector = Method.AesVector;

        static Encoding getEncode()
        {
            var en = Encoding.Default;
            var code = ConfigurationManager.AppSettings["encode"];
            if (code.IsNotEmpty())
            {
                if (code == "UTF8")
                {
                    en = Encoding.UTF8;
                }
                else
                    en = Encoding.GetEncoding(code);
            }
            return en;
        }

        /// <summary>
        /// Aes加密
        /// </summary>
        /// <param name="content">加密内容</param>
        /// <param name="key">密钥 为空则使用默认</param>
        /// <param name="vector">向量 为空则使用默认</param>
        /// <param name="replace">是否替换特定字符</param>
        /// <param name="UrlEncode">是否对url进行编码</param>
        /// <returns></returns>
        public static string AesEncrypt(this string content, string key = Aeskey, string vector = AesVector, bool replace = false, bool UrlEncode = false)
        {
            if (key.IsEmpty() || vector.IsEmpty())
            {
                throw new Exception("密钥或者向量为空");
            }
            content = EncryptProvider.AESEncrypt(content, key, vector);
            if (replace)
                content = content.Replace("/", "*").Replace("+", "_");
            if (UrlEncode)
                content = HttpContext.Current.Server.UrlEncode(content);
            return content;
        }

        /// <summary>
        /// Aes解密
        /// </summary>
        /// <param name="content">解密内容</param>
        /// <param name="key">密钥 为空则使用默认</param>
        /// <param name="vector">向量 为空则使用默认</param>
        /// <param name="replace">是否替换特定字符</param>
        /// <param name="UrlEncode">是否对url进行编码</param>
        /// <returns></returns>
        public static string AesDecrypt(this string content, string key = Aeskey, string vector = AesVector, bool replace = false, bool UrlDecode = false)
        {
            if (key.IsEmpty() || vector.IsEmpty())
            {
                throw new Exception("密钥或者向量为空");
            }
            if (replace)
                content = content.Replace("*", "/").Replace("_", "+");
            if (UrlDecode)
                content = HttpContext.Current.Server.UrlDecode(content);
            content = EncryptProvider.AESDecrypt(content, key, vector);
            return content;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Encode(this string value)
        {
            return Method.Encode(value);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Decode(this string value)
        {
            return Method.Decode(value);
        }

        /// <summary>
        /// 替换指定的字符以便php能识别
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string PHPReplace(this string value)
        {
            return value.Replace("/", "*");
        }

        /// <summary>
        /// 明文加密成数据库所用密码
        /// </summary>
        /// <param name="blankValue">待加密明文</param>
        /// <returns></returns>
        public static string getPwd(this string blankValue)
        {
            return Method.GetMD5(blankValue, Function.Method.MD5EncryptBit.bit32);
        }

        /// <summary>
        /// 是否不包含T类型中的一个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="filter">筛选器 剔除不符合条件的值</param>
        /// <param name="list">参数列表</param>
        /// <returns></returns>
        public static bool NotIn<T>(this T value, params T[] list)
        {
            var notallin = true;

            if (list.Length == 0)
            {
                notallin = true;
            }
            else
            {
                foreach (var item in list)
                {
                    if (item.Equals(value))
                    {
                        notallin = false;
                        break;
                    }
                }
            }
            return notallin;
        }

        /// <summary>
        /// 是否不包含T类型中的一个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="filter">筛选器 剔除不符合条件的值</param>
        /// <param name="list">参数列表</param>
        /// <returns></returns>
        public static bool NotIn<T>(this T value, Func<T, Boolean> filter, params T[] list)
        {
            var notallin = true;
            var emptylist = new List<T>();

            if (filter != null)
                for (int i = 0; i < list.Count(); i++)
                {
                    if (filter(list[i]))
                    {
                        continue;
                    }
                    else
                        emptylist.Add(list[i]);
                }

            if (emptylist.Count == 0)
            {
                notallin = true;
            }
            else
            {
                foreach (var item in emptylist)
                {
                    if (item.Equals(value))
                    {
                        notallin = false;
                        break;
                    }
                }
            }
            return notallin;
        }

        /// <summary>
        /// 是否包含T类型中的一个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="filter">筛选器 剔除不符合条件的值</param>
        /// <param name="list">参数列表</param>
        /// <returns></returns>
        public static bool In<T>(this T value, Func<T, Boolean> filter, params T[] list)
        {
            var allin = false;
            var emptylist = new List<T>();

            if (filter != null)
                for (int i = 0; i < list.Count(); i++)
                {
                    if (filter(list[i]))
                    {
                        continue;
                    }
                    else
                        emptylist.Add(list[i]);
                }

            if (emptylist.Count == 0)
            {
                allin = false;
            }
            else
            {
                foreach (var item in emptylist)
                {
                    if (item.Equals(value))
                    {
                        allin = true;
                        break;
                    }
                }
            }
            return allin;
        }

        /// <summary>
        /// 是否包含T类型中的一个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="filter">筛选器 剔除不符合条件的值</param>
        /// <param name="list">参数列表</param>
        /// <returns></returns>
        public static bool In<T>(this T value, params T[] list)
        {
            var allin = false;

            if (list.Length == 0)
            {
                allin = false;
            }
            else
            {
                foreach (var item in list)
                {
                    if (item.Equals(value))
                    {
                        allin = true;
                        break;
                    }
                }
            }
            return allin;
        }

        /// <summary>
        /// 打印空白的文本消息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message">打印的消息</param>
        /// <param name="clearResponse">打印消息前是否清除response的所有信息</param>
        /// <param name="endResponse">打印消息后是否结束response</param>
        public static void PostFinalMessage(this Page page, string message, bool clearResponse = true, bool endResponse = true)
        {
            if (clearResponse)
                page.Response.Clear();
            page.Response.Write(message);
            if (endResponse)
                page.Response.End();
        }

        /// <summary>
        /// 打印空白的文本消息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="message">打印的消息</param>
        /// <param name="clearResponse">打印消息前是否清除response的所有信息</param>
        /// <param name="endResponse">打印消息后是否结束response</param>
        public static void PostFinalMessage(this Page page, string message, params object[] para)
        {
            page.Response.Clear();
            page.Response.Write(string.Format(message, para));
            page.Response.End();
        }

        /// <summary>
        /// 检查拥有的权限项
        /// </summary>
        /// <param name="list"></param>
        /// <param name="checkList"></param>
        /// <returns></returns>
        public static Boolean HaveRight(this List<string> list, params string[] checkList)
        {
            return true;
        }

        /// <summary>
        /// 判断string值是否为空
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static Boolean IsEmpty(this string Value)
        {
            if (Value == null)
                return true;
            return string.IsNullOrEmpty(Value.Trim());
        }

        /// <summary>
        /// 判断string值是否为空
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static Boolean IsNotEmpty(this string Value)
        {
            if (Value == null)
                return false;
            return !string.IsNullOrEmpty(Value.Trim());
        }

        /// <summary>
        /// 判断是否是正数
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static Boolean IsNumber(this string Value)
        {
            var result = true;
            try
            {
                int.Parse(Value);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Convert datatable to string
        /// </summary>
        /// <param name="dt">datatable</param>
        /// <param name="columnIndex">columnindex</param>
        /// <returns></returns>
        public static string ToStringEx(this DataTable dt, int columnIndex, char splitter, bool removeLastChar)
        {
            if (dt.Rows.Count == 0)
                return "";
            else
            {
                var str = string.Empty;
                foreach (DataRow item in dt.Rows)
                {
                    str += item[columnIndex].ToString() + splitter;
                }
                if (removeLastChar)
                    str = str.Remove(str.Length - 1, 1);
                return str;
            }
        }

        /// <summary>
        /// 判断是否是日期
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static Boolean IsDate<T>(this T Value)
        {
            var result = true;
            try
            {
                Convert.ToDateTime(Value);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 判断是否是小数
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static Boolean IsDecimal<T>(this T Value)
        {
            var result = true;
            try
            {
                Convert.ToDecimal(Value);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 指定类型转换成时间
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static DateTime ToDate<T>(this T Value)
        {
            return DateTime.Parse(Value.ToString());
        }

        /// <summary>
        /// 得到物理路径
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToPhysicalPath(this string value)
        {
            return HttpContext.Current.Server.MapPath(value);
        }

        /// <summary>
        /// 去除\0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetClearString(this string value)
        {
            return value.Trim().Replace("\0", "");
        }

        /// <summary>
        /// 去除条件内的字符串
        /// </summary>
        /// <param name="value"></param>
        /// <param name="filters"></param>
        /// <returns></returns>
        public static string FilterString(this string value, params string[] filters)
        {
            filters.ToList().ForEach(s => value = value.Replace(s, ""));
            return value;
        }

        /// <summary>
        /// 添加加号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string RestoreAddMark(this string value)
        {
            return value.Trim().Replace(" ", "+");
        }

        /// <summary>
        /// 将list转换为特定分隔符的string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="spliter">分隔符</param>
        /// <param name="RemoveLastSpliter">移出最后的一个分隔符</param>
        /// <returns></returns>
        public static string ToSplitString<T>(this List<T> list, char spliter, bool RemoveLastSpliter)
        {
            StringBuilder str = new StringBuilder();
            list.ForEach((T s) => { str.Append(s.ToString()).Append(spliter); });
            string strResult = str.ToString();
            if (RemoveLastSpliter && strResult.Length > 1)
            {
                strResult = strResult.Remove(strResult.Length - 1, 1);
            }
            return strResult.Trim();
        }

        /// <summary>
        /// 清楚空数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<string> CleartEmtpyItem(this List<string> list)
        {
            foreach (var item in list)
            {
                if (item.IsEmpty())
                {
                    list.Remove(item);
                }
            }
            return list;
        }

        /// <summary>
        /// 将对象转换成Xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToXml<T>(this T obj) where T : class
        {
            var type = typeof(T);
            var tname = type.Name;
            var info = type.GetProperties();
            var str = new StringBuilder();
            str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            str.Append("<root>");
            str.AppendLine("<" + tname + ">");
            foreach (PropertyInfo item in info)
            {
                str.AppendFormat("<{0}>{1}</{0}>", item.Name, item.GetValue(obj, null));
            }
            str.AppendLine("</" + tname + ">");
            str.Append("</root>");
            return str.ToString();
        }

        /// <summary>
        /// 将对象数组转换成xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToXml<T>(this T[] obj) where T : class
        {
            var str = new StringBuilder();
            str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            str.Append("<root>");
            foreach (var t in obj)
            {
                var type = t.GetType();
                var tname = type.Name;
                var info = type.GetProperties();
                str.AppendLine("<" + tname + ">");
                foreach (PropertyInfo item in info)
                {
                    str.AppendFormat("<{0}>{1}</{0}>", item.Name, item.GetValue(t, null));
                }
                str.AppendLine("</" + tname + ">");
            }
            str.Append("</root>");
            return str.ToString();
        }

        /// <summary>
        /// 将对象list转换成xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToXml<T>(this List<T> obj) where T : class
        {
            var str = new StringBuilder();
            str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            str.Append("<root>");
            foreach (var t in obj)
            {
                var type = t.GetType();
                var tname = type.Name;
                var info = type.GetProperties();
                str.AppendLine("<" + tname + ">");
                foreach (PropertyInfo item in info)
                {
                    str.AppendFormat("<{0}>{1}</{0}>", item.Name, item.GetValue(t, null));
                }
                str.AppendLine("</" + tname + ">");
            }
            str.Append("</root>");
            return str.ToString();
        }

        /// <summary>
        /// 将list转换为特定分隔符的string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="spliter">分隔符</param>
        /// <param name="RemoveLastSpliter">移出最后的一个分隔符</param>
        /// <returns></returns>
        public static string ToSplitString<T>(this List<T> list, string spliter, bool RemoveLastSpliter, bool addFirsrtSpliter)
        {
            StringBuilder str = new StringBuilder();
            list.ForEach((T s) => { str.Append(s.ToString()).Append(spliter); });
            string strResult = str.ToString();
            if (RemoveLastSpliter && strResult.Length > 1)
            {
                strResult = strResult.Remove(strResult.Length - spliter.Length, spliter.Length);
            }
            if (addFirsrtSpliter && list.Count > 0)
            {
                strResult = strResult.Insert(0, spliter);
            }
            return strResult.Trim();
        }

        /// <summary>
        /// byte数组转换为string
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToByteString(this byte[] value)
        {
            return encode.GetString(value);
        }

        /// <summary>
        /// 转换指定数据到int32
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Int32 ToInt32<T>(this T value)
        {
            return Convert.ToInt32(value);
        }

        /// <summary>
        ///转换指定数据到decimal
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal<T>(this T value)
        {
            return Convert.ToDecimal(value);
        }

        /// <summary>
        /// 根据Table返回list
        /// </summary>
        /// <returns></returns>
        public static List<T> GetList<T>(this DataTable dt) where T : class
        {
            List<T> list = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                T t = (T)typeof(T).GetConstructor(Type.EmptyTypes).Invoke(null);
                foreach (PropertyInfo propertyInfo in t.GetType().GetProperties())
                {
                    if (row[propertyInfo.Name] != null)
                    {
                        if (!DBNull.Value.Equals(t))
                            propertyInfo.SetValue(t, row[propertyInfo.Name], null);
                    }
                }
                list.Add(t);
            }
            return list;
        }

        /// <summary>
        /// 将对象转换成Xml字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="writeResult">是否写入执行结果编码</param>
        /// <param name="resultCode">执行结果编码 一般0成功</param>
        /// <param name="tname">对象节点名称 适用于匿名类型</param>
        /// <param name="filter">过滤器 适用于递归加入数据</param>
        /// <returns></returns>
        public static string ToSingleXml<T>(this T obj, bool writeResult = true, int resultCode = 0, string tname = null, Func<T, string> filter = null) where T : class
        {
            var type = typeof(T);
            tname = tname ?? type.Name;
            var info = type.GetProperties();
            var str = new StringBuilder();
            str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            str.AppendLine("<root>");
            if (writeResult)
                str.AppendLine(string.Format("<result>{0}</result>", resultCode));
            if (obj == null)
                return str.AppendLine("<currentCount>0</currentCount>").AppendLine("</root>").ToString();
            str.AppendLine("<currentCount>1</currentCount>");
            str.AppendLine("<" + tname + ">");
            foreach (PropertyInfo item in info)
            {
                str.AppendFormat("<{0}>{1}</{0}>", item.Name, item.GetValue(obj, null)).AppendLine();
            }
            str.AppendLine("</" + tname + ">");
            if (filter != null)
                str.Append(filter(obj));
            str.AppendLine("</root>");
            return str.ToString();
        }

        /// <summary>
        /// datatable转换成xml
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <param name="tname"></param>
        /// <returns></returns>
        public static string DtToXml<T>(this DataTable dt, string tname = null) where T : class
        {
            return dt.GetList<T>().ToXml(tname: tname);
        }

        /// <summary>
        /// 单个字符串转换成xml
        /// </summary>
        /// <param name="content"></param>
        /// <param name="tname"></param>
        /// <param name="nodename"></param>
        /// <returns></returns>
        public static string ToXml(this string content, string tname = "root", string nodename = "node")
        {
            var str = new StringBuilder();
            str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            str.AppendLine("<" + tname + ">");
            str.AppendLine("<" + nodename + ">" + content + "</" + nodename + ">");
            str.AppendLine("</" + tname + ">");
            return str.ToString();
        }

        /// <summary>
        /// 将数组转换为xml
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">数组</param>
        /// <param name="writeResult">是否写入结果节点</param>
        /// <param name="result">结果标识</param>
        /// <param name="tname">实体名字</param>
        /// <param name="writeCurrentCount">是否写入当前数据条数</param>
        /// <param name="writeTotalCount">是否写入总条数</param>
        /// <param name="totalRecords">总共条数</param>
        /// <returns></returns>
        public static string ToXml<T>(this T[] obj, bool writeResult = true, int result = 0, string tname = null, bool writeCurrentCount = true,
            bool writeTotalCount = false, int totalRecords = 0) where T : class
        {
            var str = new StringBuilder();
            str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            str.Append("<root>");
            if (writeResult)
                str.AppendLine("<result>" + result + "</result>");
            if (writeCurrentCount)
                str.AppendLine("<currentCount>" + obj.Length + "</currentCount>");
            if (writeTotalCount)
                str.AppendLine("<totalCount>" + totalRecords + "</totalCount>");
            foreach (var t in obj)
            {
                var type = t.GetType();
                tname = tname ?? type.Name;
                var info = type.GetProperties();
                str.AppendLine("<" + tname + ">");
                foreach (PropertyInfo item in info)
                {
                    str.AppendFormat("<{0}>{1}</{0}>", item.Name, item.GetValue(t, null)).AppendLine();
                }
                str.AppendLine("</" + tname + ">");
            }
            str.AppendLine("</root>");
            return str.ToString();
        }

        /// <summary>
        /// 将列表转换为xml
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="obj"></param>
        /// <param name="tname">实体名字</param>
        /// <param name="resultCode"></param>
        /// <param name="writeCurrentCount">是否写当前的记录条数</param>
        /// <param name="writeTotalCount">是否写总的记录条数 需指定totalcount</param>
        /// <param name="writeHead">是否写xml开头标记</param>
        /// <param name="writeRoot">是否写xml结尾标记</param>
        /// <param name="rootName">根节点名称</param>
        /// <param name="totalRecords">总记录条数</param>
        /// <param name="filter">过滤器 可用于递归插入列表数据</param>
        /// <returns></returns>
        public static string ToXml<T>(this List<T> obj, bool writeResult = true, int resultCode = 0, string tname = null, bool writeCurrentCount = true, bool writeTotalCount = false, bool writeHead = true,
            bool writeRoot = true, string rootName = "root", int totalRecords = 0, Func<T, string> filter = null) where T : class
        {
            var str = new StringBuilder();
            if (writeHead)
                str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            if (writeRoot)
                str.AppendLine("<" + rootName + ">");
            if (writeResult)
                str.AppendLine("<result>" + resultCode + "</result>");
            if (writeCurrentCount)
                str.AppendLine("<currentCount>" + obj.Count + "</currentCount>");
            if (writeTotalCount)
            {
                str.AppendLine("<totalCount>" + totalRecords + "</totalCount>");
            }
            if (totalRecords > 0)
                str.AppendLine("<list>");
            foreach (var t in obj)
            {
                var type = t.GetType();
                tname = tname ?? type.Name;
                var info = type.GetProperties();
                str.AppendLine("<" + tname + ">");
                foreach (PropertyInfo item in info)
                {
                    str.AppendFormat("<{0}>{1}</{0}>", item.Name, item.GetValue(t, null)).AppendLine();
                }
                if (filter != null)
                    str.Append(filter(t));//这里不能appendLine 不然会多出一个空行
                str.AppendLine("</" + tname + ">");
            }
            if (totalRecords > 0)
                str.AppendLine("</list>");
            if (writeRoot)
                str.AppendLine("</" + rootName + ">");
            return str.ToString();
        }

        /// <summary>
        /// 字符串转换成base64字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToBase64String(this string value)
        {
            var bytes = value.GetBytes();
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// base64字符串转换成普通字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string FromBase64String(this string value)
        {
            var bytes = Convert.FromBase64String(value);
            return bytes.ToByteString();
        }

        /// <summary>
        /// string转换为byte数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this string value)
        {
            return encode.GetBytes(value);
        }

        /// <summary>
        /// 去掉字符串中指定长度的开始字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string RemoveStartChar(this string value, int length)
        {
            if (value.Length > length)
            {
                value = value.Substring(length, value.Length - length);
            }
            return value;
        }

        /// <summary>
        /// 截取第一个字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string subFirstChar(this string value)
        {
            if (value.Length > 1)
            {
                value = value.Substring(1, value.Length - 1);
            }
            return value;
        }

        /// <summary>
        /// 截取最后一个字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string subLastChar(this string value)
        {
            if (value.Length > 1)
            {
                value = value.Substring(0, value.Length - 1);
            }
            return value;
        }

        /// <summary>
        /// 得到查询字符串 默认情况下当值为空时忽略某一条件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="appendUrl"></param>
        /// <param name="ddlIgnoreValue">下拉列表忽视条件的值 默认空</param>
        /// <param name="radioIgnoreValue">单选框列表忽视条件的值 默认空</param>
        /// <param name="textboxIgnoreValue">文本框忽视条件的值 默认空</param>
        public static string QueryUrl(this HtmlForm page, string appendUrl, string textboxIgnoreValue = "", string radioIgnoreValue = "", string ddlIgnoreValue = "")
        {
            foreach (var item in page.Controls)
            {
                //文本框
                if (item is TextBox)
                {
                    var box = item as TextBox;
                    var id = box.ID;
                    var text = box.Text.Trim();
                    if (text.IsNotEmpty() && text != textboxIgnoreValue)
                    {
                        appendUrl += string.Format("&{0}={1}", id, text);
                    }
                }
                //单选按钮
                if (item is RadioButtonList)
                {
                    var box = item as RadioButtonList;
                    var id = box.ID;
                    var text = "";
                    if (box.SelectedItem != null)
                    {
                        text = box.SelectedItem.Text.Trim();
                    }
                    var value = box.SelectedValue.Trim();
                    if (value.IsNotEmpty() && value != radioIgnoreValue)
                    {
                        appendUrl += string.Format("&{0}={1}", id, value);
                    }
                    if (text.IsNotEmpty() && value != radioIgnoreValue)
                    {
                        appendUrl += string.Format("&{0}tip={1}", id, text);
                    }
                }
                //下拉列表
                if (item is DropDownList)
                {
                    var box = item as DropDownList;
                    var id = box.ID;
                    var text = "";
                    if (box.SelectedItem != null)
                    {
                        text = box.SelectedItem.Text.Trim();
                    }
                    var value = box.SelectedValue.Trim();
                    if (value.IsNotEmpty() && value != ddlIgnoreValue && Convert.ToInt32(value) != 0)
                    {
                        appendUrl += string.Format("&{0}={1}", id, value);
                    }
                    if (text.IsNotEmpty() && value != ddlIgnoreValue && !text.Equals("请选择"))
                    {
                        appendUrl += string.Format("&{0}tip={1}", id, text);
                    }
                }
            }
            return appendUrl;
        }

        /// <summary>
        /// 得到查询字符串 默认情况下当值为空时忽略某一条件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="appendUrl"></param>
        /// <param name="ddlIgnoreValue">下拉列表忽视条件的值 默认空</param>
        /// <param name="radioIgnoreValue">单选框列表忽视条件的值 默认空</param>
        /// <param name="textboxIgnoreValue">文本框忽视条件的值 默认空</param>
        public static string QueryUrlLike(this HtmlForm page, string appendUrl, string textboxIgnoreValue = "", string radioIgnoreValue = "", string ddlIgnoreValue = "")
        {
            foreach (var item in page.Controls)
            {
                //文本框
                if (item is TextBox)
                {
                    var box = item as TextBox;
                    var id = box.ID;
                    var text = box.Text.Trim();
                    if (text.IsNotEmpty() && text != textboxIgnoreValue)
                    {
                        appendUrl += string.Format("&{0} like '%{1}%'", id, text);
                    }
                }
                //单选按钮
                if (item is RadioButtonList)
                {
                    var box = item as RadioButtonList;
                    var id = box.ID;
                    var text = "";
                    if (box.SelectedItem != null)
                    {
                        text = box.SelectedItem.Text.Trim();
                    }
                    var value = box.SelectedValue.Trim();
                    if (value.IsNotEmpty() && value != radioIgnoreValue)
                    {
                        appendUrl += string.Format("&{0}={1}", id, value);
                    }
                    if (text.IsNotEmpty() && value != radioIgnoreValue)
                    {
                        appendUrl += string.Format("&{0}tip={1}", id, text);
                    }
                }
                //下拉列表
                if (item is DropDownList)
                {
                    var box = item as DropDownList;
                    var id = box.ID;
                    var text = "";
                    if (box.SelectedItem != null)
                    {
                        text = box.SelectedItem.Text.Trim();
                    }
                    var value = box.SelectedValue.Trim();
                    if (value.IsNotEmpty() && value != ddlIgnoreValue && Convert.ToInt32(value) != 0)
                    {
                        appendUrl += string.Format("&{0}={1}", id, value);
                    }
                    if (text.IsNotEmpty() && value != ddlIgnoreValue && !text.Equals("请选择"))
                    {
                        appendUrl += string.Format("&{0}tip={1}", id, text);
                    }
                }
            }
            return appendUrl;
        }
        /// <summary>
        /// 表单控件空值
        /// </summary>
        /// <param name="page">检查页面</param>
        /// <param name="ignoreIDList">忽略检查的控件id</param>
        /// <param name="alertProperty">为空时警告读取的属性时</param>
        /// <returns></returns>
        public static string ValidateForm(this HtmlForm page, out bool ifalert, string ignoreIDList = "", string alertProperty = "placeholder")
        {
            ifalert = false;
            var alertMsg = "";
            foreach (var item in page.Controls)
            {
                //文本框
                if (item is TextBox)
                {
                    var box = item as TextBox;
                    var id = box.ID;
                    var text = box.Text.Trim();
                    //检查是否忽略
                    if (ignoreIDList.Contains(id))
                        continue;
                    if (text.IsEmpty())
                    {
                        ifalert = true;
                        if (box.Attributes[alertProperty] != null)
                        {
                            alertMsg = box.Attributes[alertProperty].ToString();
                        }
                        break;
                    }
                }
                //单选按钮
                if (item is RadioButtonList)
                {
                    var box = item as RadioButtonList;
                    var id = box.ID;

                    //检查是否忽略
                    if (ignoreIDList.Contains(id))
                        continue;
                    if (box.SelectedValue.IsEmpty())
                    {
                        ifalert = true;
                        if (box.Attributes[alertProperty] != null)
                        {
                            alertMsg = box.Attributes[alertProperty].ToString();
                        }
                        break;
                    }
                }
                //下拉列表
                if (item is DropDownList)
                {
                    var box = item as DropDownList;
                    var id = box.ID;

                    //检查是否忽略
                    if (ignoreIDList.Contains(id))
                        continue;
                    if (box.SelectedValue.IsEmpty())
                    {
                        ifalert = true;
                        if (box.Attributes[alertProperty] != null)
                        {
                            alertMsg = box.Attributes[alertProperty].ToString();
                        }
                        break;
                    }
                }
            }
            if (alertMsg.IsEmpty())
                alertMsg = "请将数据填写完整";
            else
                alertMsg = "数据项 [<span style='color:red'>" + alertMsg + "</span>] 不能为空";
            return alertMsg;
        }
    }
}
